<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态生成可编辑表格</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #inputSec{
            display: flex;
            width: 1000px;
            height: 24px;
            margin: 10px 0 10px 10px;
        }
        #inputSec input{
            margin-right: 10px;
        }
        table{
            min-height: 10px;
            border-spacing: 0;
            border-collapse: collapse;
            width: 100%;
            table-layout: fixed;
        }
        table td{
            border: 1px solid blue;
            height: 40px;
            text-align: center;
            line-height: 40px;
        }
        table td input{
            width: 100%;
            height: 100%;
            display: block;
            border: none;
            padding: 0 20px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <section id='inputSec'>
        <label for="">姓名</label><input type="text" id='theName'>
        <label for="">年龄</label><input type="text" id='age'>
        <label for="">电话</label><input type="text" id='phone'>
        <label for="">QQ号</label><input type="text" id='qq'>
        <button class="saveBtn">保存</button>
    </section>
    <section id='outputSec'>
        <table>
            <table>
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>姓名</td>
                        <td>年龄</td>
                        <td>电话</td>
                        <td>QQ号</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>1</td>
                        <td class="edit">老王</td>
                        <td class="edit">24</td>
                        <td class="edit">12345678901</td>
                        <td class="edit">888888888</td>
                        <td>
                            <button class="delBtn">删除</button>
                        </td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td class="edit">老李</td>
                        <td class="edit">28</td>
                        <td class="edit">12345678902</td>
                        <td class="edit">999999999</td>
                        <td>
                            <button class="delBtn">删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </table>
    </section>

    <script>
        // 获取元素
        var theName = document.querySelector('#theName');
        var age = document.querySelector('#age');
        var phone = document.querySelector('#phone');
        var qq = document.querySelector('#qq');
        var saveBtn = document.querySelector('.saveBtn');
        var tbody = document.querySelector('tbody');
        // console.log(theName,age,phone,qq,saveBtn);

        var idNum = 3;

        // 删除封装，以处理保存前和保存后都能进行删除操作
        function delTr(){
            var delBtns = document.querySelectorAll('.delBtn');
            delBtns.forEach(function(item,index){
                item.onclick = function(){
                    item.parentNode.parentNode.remove();
                }
            });
        }
        delTr();

        /* 可编辑：未用事件委托的处理方法
                1）创建input
                2）td的值赋值给input
                3）清空td后插入input
                4）失焦后input值重新赋值给td
        */
        // 可编辑封装
        function editTr(){
            var edits = document.querySelectorAll('.edit');
            edits.forEach(function(item,index){
                // 绑定点击
                item.onclick = function(){
                    // 创建input
                    var input = document.createElement('input')
                    // 赋值
                    input.value = item.innerHTML;
                    // 清空插入
                    item.innerHTML = '';
                    item.appendChild(input);
                    // 聚焦
                    input.focus();
                    input.style.background = '#ccc';

                    // 失焦
                    input.onblur = function(){
                        item.innerHTML = input.value;
                    }
                }
            });
        }
        editTr();

        // 保存新数据
        saveBtn.onclick = function(){
            // 获取数据
            var _name = theName.value;
            var _age = age.value;
            var _phone = phone.value;
            var _qq = qq.value;

            tbody.innerHTML += '<tr>' +
                        '<td>'+idNum+'</td>' + 
                        '<td class="edit">'+_name+'</td>' +
                        '<td class="edit">'+_age+'</td>' +
                        '<td class="edit">'+_phone+'</td>' +
                        '<td class="edit">'+_qq+'</td>' +
                        '<td>' + 
                            '<button class="delBtn">删除</button>' + 
                        '</td>' + 
                    '</tr>'
            idNum++;

            // 在新保存数据后获取所有的删除按钮
            delTr();

            // 可编辑处理
            editTr();
        }
    </script>
</body>
</html>